
//
//  NoteItemViews.m
//  DogeLegs
//
//  Created by 宋华 on 2017/8/19.
//  Copyright © 2017年 宋华. All rights reserved.
//

#import "NoteItemViews.h"
#import "CustomNoteImageview.h"
#import "AddNoteButton.h"
#import "ImageMode.h"


@interface NoteItemViews () {


}


@property (nonatomic,copy)NSArray *items;

@property (nonatomic,strong)NSMutableArray *subitems;

@property (nonatomic,assign)BOOL isAddNote;

@property (nonatomic,assign)CGPoint topLeftPoint;


@property (nonatomic,assign)NSInteger itemsCount;

@property (nonatomic,assign)NSInteger allRowCount;

@end


@implementation NoteItemViews

//isAddNote:true这种是post时候用的，false正常朋友权展示
- (id)initWithItems:(NSArray *)items topLeftPoint:(CGPoint)point isAddNote:(BOOL)isAddNote {
    if (self = [super init]) {
        self.isAddNote = isAddNote;
        self.topLeftPoint  = point;
        [self reloadItems:items];
    }
    return self;
}

- (void)reloadItems:(NSArray *)items topLeftPoint:(CGPoint)point isAddNote:(BOOL)isAddNote {
    self.topLeftPoint = point;
    self.isAddNote = isAddNote;
    [self reloadItems:items];
}

- (void)reloadItems:(NSArray *)items {
    if (self.items != items) {
        WeakSelf(ws);
        self.items = items;
        for ( UIView *subview in self.subitems) {
            [subview removeFromSuperview];
        }
        [self.subitems removeAllObjects];
        
        for (NSInteger i = 0 ; i < self.itemsCount ;i++) {
            NSInteger rowIndex = i /kColumn;
            NSInteger columIndex = i%kColumn;
            CGFloat originx = columIndex *ItemsWidth + columIndex*kItem_X_space;
            CGFloat originY = rowIndex *ItemsWidth + rowIndex *kItem_Y_space;
            UIView *itemView = nil;
            if (i < self.items.count) {
               itemView = [[[NSBundle mainBundle] loadNibNamed:@"CustomNoteImageview" owner:nil options:nil] lastObject];
                CustomNoteImageview *imageview = (CustomNoteImageview *)itemView;
                imageview.imageIndex = i;
                imageview.isShowDeleteBtn = self.isAddNote;
                imageview.deletePicBlock = ^(NSInteger index) {
                    if (ws.deletePicBlock) {
                        ws.deletePicBlock(index);
                    }
                };
                [imageview setImage:self.items[i]];
                
            } else {
                itemView = [[[NSBundle mainBundle] loadNibNamed:@"AddNoteButton" owner:nil options:nil] lastObject];
                AddNoteButton *noteBtn = (AddNoteButton *)itemView;
                noteBtn.addPicBlock = ^() {
                    if (ws.addPicBlock) {
                        ws.addPicBlock();
                    }
                };
            }
            
            CGFloat itemWidth = ItemsWidth;
            CGFloat itemHeight = itemWidth;
            if (self.items.count == 1 && !self.isAddNote) {//只有一张图片且非添加的时候
                ImageMode *imgMode = self.items[0];
                CGSize itemSize = onlyPictureDisplaySize(imgMode.imageWidth,imgMode.imageHeight);
                itemWidth = itemSize.width;
                itemHeight = itemSize.height;
            }
            itemView.frame = CGRectMake(originx, originY, itemWidth, itemHeight);
            [self addSubview:itemView];
            [self.subitems addObject:itemView];
        }
        CGFloat seWidth = kColumn *ItemsWidth +(kColumn -1)*kItem_X_space;

        CGFloat seHeight = [self totalHeight];
        
        //对于xib无效，只对纯代码有效
        if (!self.isAddNote) {
            self.frame = CGRectMake(_topLeftPoint.x, _topLeftPoint.y, seWidth, seHeight);
        }
    }
}

#pragma mark - getter
- (NSInteger)itemsCount {
    NSInteger itemCount = self.items.count;
    if (self.isAddNote && self.items.count < kNotePictureMaxNum) {//九张之后不显示+号的了
        itemCount +=1;
    }
    return itemCount;
}

- (NSInteger)allRowCount {
    NSInteger allRowCount = (self.itemsCount - 1)/kColumn + 1;
    return allRowCount;
}

- (CGFloat)totalWidth {
    CGFloat itemWidth = 0;
    if (self.items.count == 1) {//只有一张图片
        ImageMode *imgMode = self.items[0];
        CGSize itemSize = onlyPictureDisplaySize(imgMode.imageWidth,imgMode.imageHeight);
        itemWidth = itemSize.width;
    } else if (self.items.count > 1){
        itemWidth = kNotePicsMaxWidth;
    }
    return itemWidth;
}

- (CGFloat)totalHeight {
    CGFloat itemHeight = 0;
    if (self.items.count == 1) {//只有一张图片
        ImageMode *imgMode = self.items[0];
        CGSize itemSize = onlyPictureDisplaySize(imgMode.imageWidth,imgMode.imageHeight);
        itemHeight = itemSize.height;
    } else if (self.items.count > 1){
       itemHeight = self.allRowCount *ItemsWidth + (self.allRowCount -1) *kItem_Y_space;
    }
    return itemHeight;
}

+ (CGSize)calculateTotalSize:(NSArray *)noteImgs {
    if (noteImgs.count == 0) {
        return CGSizeZero;
    }
    CGFloat itemWidth = 0;
    NSInteger imgCount = noteImgs.count;
    if (imgCount == 1) {//只有一张图片
        ImageMode *imgMode = noteImgs[0];
        CGSize itemSize = onlyPictureDisplaySize(imgMode.imageWidth,imgMode.imageHeight);
        itemWidth = itemSize.width;
    } else if (imgCount > 1){
        itemWidth = kNotePicsMaxWidth;
    }
    
    CGFloat itemHeight = 0;
    if (imgCount == 1) {//只有一张图片
        ImageMode *imgMode = noteImgs[0];
        CGSize itemSize = onlyPictureDisplaySize(imgMode.imageWidth,imgMode.imageHeight);
        itemHeight = itemSize.height;
    } else if (imgCount > 1){
        NSInteger allRowCount = (imgCount - 1)/kColumn + 1;
        itemHeight = allRowCount *ItemsWidth + (allRowCount -1) *kItem_Y_space;
    }
    return CGSizeMake(itemWidth, itemHeight);
    
}

- (NSMutableArray *)subitems {
    if (_subitems == nil) {
        _subitems = [NSMutableArray array];
    }
    return _subitems;
}


@end
